/*
write by wuweiwei
*/


var wDragger = function(options){
	/*
	options = {
		container:node,
		dragElement:"xx", this is html tag
		dragElementStyle:{},
		nonElement:"input,label,.className"
	}
	*/
	this.options = options;
	this.container = this.options.container;
	this.ele = (this.options.dragElement).toUpperCase(); //this is string like "tr"
	if(!!!options.dragElementStyle)
	{
		options.dragElementStyle = {};
	}
	this.dragP = {};
	this.setDragBox();
	this.initDrag();

	this.moveValue = -1;
	this.exChangeIndex = -1;
	/*
	this.dragP = {
		ele:node,
		exChangeEle:node
		startY:n,
		isDrag:boolean,
		nextSibling:node,
		nextSibling2:node,
		eleHeight:n,
		moveValue:n 用于防抖
	}
	*/
}
wDragger.prototype.getEvent = function(e){
	return e||window.event;
}
wDragger.prototype.ifNonElement = function(node){
	var nonElement = this.options.nonElement;
	if(!!!nonElement)
	{
		return false;
	}
	if(nonElement.indexOf(node.nodeName)>-1)
	{
		return true;
	}
	if(nonElement.indexOf("."+node.ClassName)>-1)
	{
		return true;
	}
	return false;
}
wDragger.prototype.addDragNodeStyle = function(node){
	var v,dragElementStyle;
	dragElementStyle = this.options.dragElementStyle;
	for(v in dragElementStyle)
	{
		node.style[v] = dragElementStyle[v];
	}
}
wDragger.prototype.removeDragNodeStyle = function(node){
	var v,dragElementStyle;
	dragElementStyle = this.options.dragElementStyle;
	for(v in dragElementStyle)
	{
		node.style[v] = "";
	}
}
wDragger.prototype.setDragBox = function(){
	var i,children = this.container.childNodes;
	var n = 0;
	for(i=0;i<children.length;i++)
	{
		if(children[i].nodeType==1)
		{
			children[i].setAttribute("index",n++);
			children[i].style.position = "relative";
			children[i].style.top = "0px";
		}
	}
}

wDragger.prototype.initDrag = function(){
	var th = this;
	var getTr = function(ele){
		if(th.container.contains(ele))
		{
			while(ele.nodeName!=th.ele)
			{
				ele = ele.parentNode;
			}
			return ele;
		}
		return null;
	}

	this.container.onmousedown = function(e){
		var evt = th.getEvent(e);
		var ele;
		ele = getTr(evt.target||evt.srcElement);

		if(th.ifNonElement(evt.target||evt.srcElement))
		{
			return;
		}
		if(!!ele)
		{
			th.dragP.moveValue = 0;
			th.addDragNodeStyle(ele);
			th.dragP.ele = ele;
			th.dragP.startX = evt.pageX;
			th.dragP.startY = evt.pageY;
			th.dragP.isDrag = true;
			th.dragP.eleHeight = ele.offsetHeight;
			th.dragP.ele.style.zIndex = 1000;
            document.onselectstart = function() {
                return ! 1;
            }
		}
	}
	this.container.onmouseup = function(e){
		var evt = th.getEvent(e);
		if(th.ifNonElement(evt.target||evt.srcElement))
		{
			return;
		}
		if(!th.dragP.isDrag)
		{
			return;
		}
		th.dragP.isDrag = false;
		th.setDragBox();
		th.removeDragNodeStyle(th.dragP.ele);
		th.dragP.ele.style.zIndex = "";
		if(th.dragP.moveValue<10)
		{
			return;
		}
		if(!!th.dragP.nextSibling)
		{
			th.container.insertBefore(th.dragP.ele,th.dragP.nextSibling);
		}
		else
		{
			th.container.appendChild(th.dragP.ele);
		}
        document.onselectstart = function() {
            return ! 0;
        }
	}
	this.container.onmousemove = function(e){
		var evt = th.getEvent(e);
		var ele,pageY,diffY;
		ele = th.dragP.ele;
		var getStringValue = function(str){
			var v = parseInt(str);
			if(isNaN(v))
			{
				return 0;
			}
			else
			{
				return v;
			}
		}

		if(th.dragP.isDrag)
		{
			th.exChange({
				evt:evt
			});
		}
	}
}
wDragger.prototype.exChange = function(options){
	/*
	return {
		ele:dom,
		index:n,
	}
	*/
	var th = this;
	var evt = options.evt;
	var ele;
	var index,height,exChangeIndex;

	var animation = function(index,exChangeIndex,exChangeEle){
		var fix =-1;
		var diff = exChangeIndex;
		if(th.dragP.ele==exChangeEle)
		{
			return;
		}

		if(index>exChangeIndex)
		{
			fix = 1;
		}
		else
		{
			fix = -1;
		}

		console.log(index,exChangeIndex);
		th.moveValue = diff;
		exChangeEle.style.top = (fix*th.dragP.eleHeight)+"px";
	}

	var getNodeFromIndex = function(index){
		var i,children = th.container.childNodes;
		var n = 0;

		for(i=0;i<children.length;i++)
		{
			if(children[i].nodeType==1)
			{
				if(n==index)
				{
					return children[i];
				}
				n++;
			}
		}
		return null;
	}
	var getIndexFromNode = function(node){
		var i,children = th.container.childNodes;
		var n = 0;
		for(i=0;i<children.length;i++)
		{
			if(children[i].nodeType==1)
			{
				if(children[i]==node)
				{
					return n;
				}
				n++;
			}
		}
	}
	ele = this.dragP.ele;
	height = ele.offsetHeight;
	index = getIndexFromNode(ele);
	this.dragP.x = evt.pageX;
	this.dragP.y = evt.pageY;
	this.dragP.diffY = this.dragP.y - this.dragP.startY;
	ele.style.top = this.dragP.diffY + "px"; /*只展示移动效果不是最终定位*/
	
	exChangeIndex = parseInt((index*height+this.dragP.diffY)/height);

	this.dragP.exChangeEle = getNodeFromIndex(exChangeIndex);

	//animation(index,exChangeIndex,this.dragP.exChangeEle);

	th.dragP.moveValue++;
	console.log(th.dragP.moveValue);

	if(!!!this.dragP.exChangeEle)
	{
		this.dragP.nextSibling = null;
		return;
	}
	try
	{
		if(this.dragP.exChangeEle.nextSibling.nodeType==1)
		{
			this.dragP.nextSibling = (th.dragP.exChangeEle.nextSibling);
		}
		else
		{
			this.dragP.nextSibling = (th.dragP.exChangeEle.nextSibling.nextSibling);
		}
	}
	catch(e)
	{
		this.dragP.nextSibling = getNodeFromIndex(th.dragP.exChangeEle);
	}

	if(parseInt(index*height+this.dragP.diffY) <= 0)
	{
		this.dragP.nextSibling = (th.dragP.exChangeEle);
	}
}

wDragger.prototype.unDrag = function(){
	this.container.onmousemove = null;
	this.container.onmouseup = null;
	this.container.onmousedown = null;
}

/*
var drag = new wDragger({
	container:document.getElementById("tbody"),
	dragElement:"tr",
	dragElementStyle:{"background-color":"#ccc"},
	nonElement:"INPUT"
});
*/